package com.wny.backend;

import android.location.Location;
import java.lang.StringBuilder;

//User class is responsible for parasing user data received from the server.
public class User implements FormattedDisplay {
	private static String delim = "/";
	private static String tableName = "users";
	private static String authTableName = "user_auth";
	private static String usernameField = "username";

	// name,email,coordinates,gender, UID, and user status are all member
	// varialbes
	private String name;
	private String email;
	private String latitude;
	private String longitude;
	private Location L;
	private int gender;
	private int uid;
	private String status;

	// get functions to return user attributes
	public String getTheUserName() {
		return name;
	}

	public String getSex() {
		if (gender == 0) {
			return "Male";
		}
		return "Female";
	}

	public String getEmail() {
		return email;
	}

	public String getStatus() {
		return status;
	}

	public String getLatitude() {
		if(latitude.equals("null"))
			return "0";
		else
		return latitude;
	}

	public String getLongitude() {
		if(longitude.equals("null"))
			return "0";
		else
		return longitude;
	}

	// Constructor for clahss. Parses input
	public User(String attributeList) {
		String[] attributes = attributeList.split(delim);
		name = attributes[1];
		uid = Integer.valueOf(attributes[0]);
		if (attributes[3].equals("m"))
			gender = 0;
		else
			gender = 1;
		email = attributes[4];
		status = attributes[5];
		latitude = attributes[6];
		longitude = attributes[7];

	}

	public String[] displayStrings() {
		// TODO Auto-generated method stub
		String[] toReturn = { name, email };
		return toReturn;
	}

	public int imageID() {
		return 0;
	};

	public int userID() {
		return uid;
	}

	public int eventID() {
		return -1;
	}

	/*
	 * Returns a byte representation of a user object. This is used for platform
	 * independent serialization of user objects.
	 */
	public static byte[] makeUserRepresentation(String username,
			String password, String name, String email, String sex) {

		StringBuilder tempString = new StringBuilder("adu,");
		tempString.append(username + delim + password + delim + name + delim
				+ email + delim + sex);
		return (tempString.toString().getBytes());
	}

	/*
	 * Makes a string representing the query required to add the user to the
	 * user table.
	 */
	public static String makeSqlCreation(String userRep) {
		StringBuilder tempString = new StringBuilder("insert into");
		String[] args = userRep.split("" + delim);
		String username = args[0];
		String name = args[2];
		String email = args[3];
		String sex = args[4];
		tempString.append(tableName + "values ('" + username + "' '" + name
				+ "' '" + email + "' '" + sex + "'");
		return tempString.toString();
	}

	/*
	 * Makes a string for the query to check for duplicate users
	 */
	public static String checkExistance(String userRep) {
		StringBuilder tempString = new StringBuilder("select * from ");
		String[] args = userRep.split("" + delim);
		tempString.append(authTableName + "where " + usernameField + "= '"
				+ args[0] + "'");
		return tempString.toString();
	}

	/*
	 * Inserts the user into the authentication table
	 */
	public static String makeSqlInsertion(String userRep) {
		StringBuilder tempString = new StringBuilder("insert into ");
		String[] args = userRep.split("" + delim);
		tempString.append(authTableName + "values ('" + args[0] + "' '"
				+ args[1] + "'");
		return tempString.toString();
	}

}
